from django.db import models
from apps.base.models import *
from django.contrib.auth import get_user_model

User = get_user_model()

unit = (('片', '片'), ('平方米', '平方米'))


class SupplierPicture(models.Model):
    def upload_to(self, filename):
        return f'images/supplier/{self.F001.B001}/{filename}'

    F001 = models.ForeignKey(Supplier, on_delete=models.CASCADE)
    Image = models.ImageField(upload_to=upload_to)

    class Meta:
        verbose_name = '供应商照片'
        verbose_name_plural = verbose_name


class SupplierQuotation(models.Model):
    A001 = models.CharField(max_length=12, primary_key=True, blank=True, verbose_name='报价单编号')
    F002 = models.ForeignKey(ProductSource, on_delete=models.CASCADE, verbose_name='产品')

    B001 = models.FloatField(verbose_name='含税单价')
    B002 = models.CharField(max_length=5, choices=unit, verbose_name='单位')
    B003 = models.CharField(max_length=10, blank=True, null=True, verbose_name='审核人')
    B004 = models.CharField(max_length=30, blank=True, null=True, verbose_name='兰庆型号')
    B005 = models.CharField(max_length=30, blank=True, null=True, verbose_name='备注')

    def __str__(self):
        return self.A001

    def addId(self):
        quotation = SupplierQuotation.objects.all().order_by('A001').last()
        self.A001 = 'SQ' + str(int(quotation.A001[2:]) + 1).zfill(6)

    def supplier(self):
        return self.F002.F002

    supplier.short_description = '供应商'

    class Meta:
        verbose_name = '供应商报价单'
        verbose_name_plural = verbose_name


class PurchaseMaster(models.Model):
    A001 = models.CharField(max_length=14, primary_key=True, verbose_name='采购单号')
    A002 = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')  # 创建时间
    A003 = models.DateField(verbose_name='下单时间')
    F001 = models.CharField(max_length=10, null=True, verbose_name='创建员工')  # 创建员工
    F002 = models.ForeignKey(Supplier, on_delete=models.CASCADE, verbose_name='供应商')

    def __str__(self):
        return self.A001

    class Meta:
        verbose_name = '采购单主表'
        verbose_name_plural = verbose_name


class PurchaseDetail(models.Model):
    F001 = models.ForeignKey(PurchaseMaster, on_delete=models.CASCADE, verbose_name='采购单主表')
    F002 = models.ForeignKey(SupplierQuotation, on_delete=models.CASCADE, null=True, verbose_name='供应商报价单')
    B001 = models.IntegerField(verbose_name='宽')
    B002 = models.IntegerField(verbose_name='长')
    B003 = models.IntegerField(verbose_name='数量(单位卷或片)')
    B004 = models.DateField(verbose_name='交期')
    B005 = models.CharField(max_length=30, null=True, blank=True, verbose_name='备注')

    def __str__(self):
        return self.F001.A001

    def amount(self):
        return str(self.B003) + self.F002.B002

    amount.short_description = '数量'

    def surplus(self):
        receipts = Receipt.objects.filter(F001=self.F001)
        search_dict = {'B001': self.B001, 'B002': self.B001, 'F002': self.F002.F002}
        receipts=receipts.filter(**search_dict)
        if receipts.count():
            total = receipts.aggregate(models.Sum('B003'))['B003__sum']
            return str(self.B003-total)
        else:
            return str(self.B003)
    surplus.short_description = '剩余未交量'

    class Meta:
        verbose_name = '采购单从表'
        verbose_name_plural = verbose_name


class SampleStock(models.Model):
    A002 = models.DateTimeField(auto_now_add=True, verbose_name='制表时间')
    F001 = models.CharField(max_length=10, null=True, blank=True, verbose_name='制表人')
    F002 = models.ForeignKey(ProductSource, on_delete=models.SET_NULL, null=True, verbose_name='料号')
    B001 = models.IntegerField(null=True, verbose_name='宽度')
    B002 = models.IntegerField(null=True, verbose_name='长度')
    B003 = models.IntegerField(null=True, verbose_name='数量')
    B004 = models.CharField(max_length=5, null=True, verbose_name='单位')
    B005 = models.CharField(max_length=50, null=True, verbose_name='备注')

    # 定义计算字段
    def specs(self):
        return f'{self.B001}*{self.B002}'

    specs.short_description = '规格'

    def amount(self):
        return f'{self.B003}{self.B004}'

    amount.short_description = '数量'

    def __str__(self):
        return self.F002.A001

    class Meta:
        verbose_name = '样品库存'
        verbose_name_plural = verbose_name


class Receipt(models.Model):
    F001 = models.ForeignKey(PurchaseMaster, on_delete=models.CASCADE, verbose_name='采购单号')
    F002 = models.ForeignKey(ProductSource, on_delete=models.CASCADE, verbose_name='产品料号')
    A001 = models.CharField(max_length=20, null=True,verbose_name='送货单号')
    B001 = models.IntegerField(verbose_name='宽')
    B002 = models.IntegerField(verbose_name='长')
    B003 = models.IntegerField(verbose_name='数量')

    def __str__(self):
        return self.A001

    def supplier(self):
        return self.F002.F002.A001

    supplier.short_description = '供应商'

    class Meta:
        verbose_name = '收货单'
        verbose_name_plural = verbose_name
